<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class ReviewsRatingsApiController extends AbstractRestfulController
{
    public function getList()
    {
		header('Access-Control-Allow-Origin: *');
    }
    public function get($user_id)
    {
		header('Access-Control-Allow-Origin: *');
		$reviwesTable=$this->getServiceLocator()->get('Models\Model\ReviewsFactory');
		$explodeId=explode('-',$user_id);
		$userId=$explodeId[0];
		$getReviewsAndRatings = $reviwesTable->getReviewsAndRatings($userId)->toArray();
		if(count($getReviewsAndRatings)!=0){
			return new JsonModel(array(
				'output'	        => 'success',
				'value'	            => 1,
				'result'			=> $getReviewsAndRatings,
			));
		}else{
			return new JsonModel(array(
				'output'		=> 'fail',
				'value'	        => 0,
			));
		}
    }
    public function create($reviewAndRating)
    {
		header('Access-Control-Allow-Origin: *');
		$userMedicineOrderTable=$this->getServiceLocator()->get('Models\Model\UserMedicineOrderFactory');	
		$checkOrderId = $userMedicineOrderTable->checkOrder($reviewAndRating['tracking_id'],$reviewAndRating['user_id']);
		$notificationTable=$this->getServiceLocator()->get('Models\Model\NotificationsFactory');
		$orderDeliveryTable=$this->getServiceLocator()->get('Models\Model\OrderDeliveryDetailsFactory');
		if($checkOrderId!=0){
			$reviwesTable=$this->getServiceLocator()->get('Models\Model\ReviewsFactory');			
			$ratingTable=$this->getServiceLocator()->get('Models\Model\RatingFactory');
			$postReview = $reviwesTable->postReview($reviewAndRating);
			$postRating = $ratingTable->postRating($reviewAndRating,$postReview);
			$getComplaintshopId=$orderDeliveryTable->getComplaintshopId($reviewAndRating['tracking_id'])->toArray();
			if(count($getComplaintshopId)!=0){
				$sendNotification=$notificationTable->sendShopComplaint($reviewAndRating,"2","0",$getComplaintshopId[0]['shop_id'],"0","3");
			}else{
				$sendNotification=$notificationTable->sendShopComplaint($reviewAndRating,"2","0",'0',"0","3");
			}
			$sendNotification=$notificationTable->sendAdminComplaint($reviewAndRating,"2","0","0","0","1");
			if($postRating!=0){
				return new JsonModel(array(
					'output'		=> 'success',
					'value'		=> 1,
				));
			}
		}else{
			return new JsonModel(array(
				'output'	=> 'fail',
				'value'		=> 0,
			));
		}
    }
    public function update($id, $s_eamil)
    {
		
    }
    public function delete($id)
    {
       
    }
}